﻿namespace AnyOffice.Components
{
    using System;
    using System.Text;

    public class VmlControl
    {
        private int _id;
        private int _index;
        private bool _isView;
        private int _nextPrcsId;
        private int _prcsCount;
        private int _prcsId;
        private string _prcsName = string.Empty;
        private string _prcsTo = string.Empty;
        private int _setLeft;
        private int _setTop;
        private StringBuilder stringBuilder = new StringBuilder();

        private string flowEnd(string prcsTo)
        {
            if (prcsTo == string.Empty) return string.Empty;
            prcsTo = "," + prcsTo + ",";
            prcsTo = prcsTo.Replace(",0,", ",结束,");
            if (prcsTo.StartsWith(",")) prcsTo = prcsTo.Substring(1);
            if (prcsTo.EndsWith(",")) prcsTo = prcsTo.Substring(0, prcsTo.Length - 1);
            return prcsTo;
        }

        public string GetVml()
        {
            this.GetVmlGraph();
            return this.stringBuilder.ToString();
        }

        private void GetVmlGraph()
        {
            this.stringBuilder.Append("<vml:roundrect inset='2pt,2pt,2pt,2pt'");
            this.stringBuilder.Append(" id='" + this._prcsId.ToString() + "'");
            this.stringBuilder.Append(" table_id='" + this._id.ToString() + "'");
            if (this._index == 0)
                this.stringBuilder.Append(" flowType='start'");
            else if (this._prcsTo.IndexOf("0") >= 0 || this._index == this._prcsCount - 1)
                this.stringBuilder.Append(" flowType='end'");
            else
                this.stringBuilder.Append(" flowType=''");
            this.stringBuilder.Append(" passCount='0'");
            this.stringBuilder.Append(" flowTitle='<b>" + this._prcsId.ToString() + "</b><br>" + this._prcsName + "'");
            this.stringBuilder.Append(" flowFlag='0' readOnly='0' receiverID='' receiverName=''");
            if (this._index == 0)
                this.stringBuilder.Append(" fillcolor='#00EE00'");
            else if (this._prcsTo.IndexOf("0") >= 0 || this._index == this._prcsCount - 1)
                this.stringBuilder.Append(" fillcolor='#F4A8BD'");
            else
                this.stringBuilder.Append(" fillcolor='#EEEEEE'");
            if (!this._isView) this.stringBuilder.Append(" onDblClick='Edit_Process(" + this._id.ToString() + ");'");
            if (this._setLeft == 0) this._setLeft = this._index / 2 * 180 + 20;
            if (this._setTop == 0) this._setTop = this._index % 2 * 180 + 50;
            this.stringBuilder.Append(" style='LEFT: " + this._setLeft + ";");
            this.stringBuilder.Append(" TOP:" + this._setTop + ";");
            this.stringBuilder.Append(" WIDTH: 100; POSITION: absolute; HEIGHT: 50;vertical-align:middle;CURSOR:hand;TEXT-ALIGN:center;z-index:1'");
            this.stringBuilder.Append(" arcsize='4321f' coordsize='21600,21600'>\n");
            this.stringBuilder.Append("<vml:shadow on='T' type='single' color='#b3b3b3' offset='3px,3px'/>\n");
            this.stringBuilder.Append("<vml:textbox inset='1pt,2pt,1pt,1pt' onselectstart='return false;' title='步骤名称：" + this._prcsName + "\n下一步骤：" + this.flowEnd(this._prcsTo) + "'><b>" + this._prcsId.ToString() + "</b><br>" + this._prcsName + "</vml:textbox>\n");
            this.stringBuilder.Append("</vml:roundrect>\n");
            if (this._prcsTo == string.Empty)
            {
                if (this._index < this._prcsCount - 1)
                {
                    this.stringBuilder.Append("<vml:line mfrID='" + this._prcsId.ToString() + "'");
                    this.stringBuilder.Append(" title=''");
                    this.stringBuilder.Append(" source='" + this._prcsId.ToString() + "'");
                    this.stringBuilder.Append(" object='" + this._nextPrcsId.ToString() + "'");
                    this.stringBuilder.Append(" from='0,0' to='0,0' style='position:absolute;display:none;z-index:2' arcsize='4321f' coordsize='21600,21600'>\n");
                    this.stringBuilder.Append("<vml:stroke endarrow='block'></vml:stroke>\n");
                    this.stringBuilder.Append("<vml:shadow on='T' type='single' color='#b3b3b3' offset='1px,1px'/>\n");
                    this.stringBuilder.Append("</vml:line>\n");
                }
            }
            else
            {
                string[] strArray = this._prcsTo.Split(new char[] { ',' });
                if (strArray.Length > 0)
                {
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        this.stringBuilder.Append("<vml:line mfrID='" + this._prcsId.ToString() + "'");
                        this.stringBuilder.Append(" title=''");
                        this.stringBuilder.Append(" source='" + this._prcsId.ToString() + "'");
                        this.stringBuilder.Append(" object='" + strArray[i] + "'");
                        this.stringBuilder.Append(" from='0,0' to='0,0' style='position:absolute;display:none;z-index:2' arcsize='4321f' coordsize='21600,21600'>\n");
                        this.stringBuilder.Append("<vml:stroke endarrow='block'></vml:stroke>\n");
                        this.stringBuilder.Append("<vml:shadow on='T' type='single' color='#b3b3b3' offset='1px,1px'/>\n");
                        this.stringBuilder.Append("</vml:line>\n");
                    }
                }
            }
        }

        public int Id
        {
            get
            {
                return this._id;
            }
            set
            {
                this._id = value;
            }
        }

        public int Index
        {
            get
            {
                return this._index;
            }
            set
            {
                this._index = value;
            }
        }

        public bool IsView
        {
            get
            {
                return this._isView;
            }
            set
            {
                this._isView = value;
            }
        }

        public int NextPrcsId
        {
            get
            {
                return this._nextPrcsId;
            }
            set
            {
                this._nextPrcsId = value;
            }
        }

        public int PrcsCount
        {
            get
            {
                return this._prcsCount;
            }
            set
            {
                this._prcsCount = value;
            }
        }

        public int PrcsId
        {
            get
            {
                return this._prcsId;
            }
            set
            {
                this._prcsId = value;
            }
        }

        public string PrcsName
        {
            get
            {
                return this._prcsName;
            }
            set
            {
                this._prcsName = value;
            }
        }

        public string PrcsTo
        {
            get
            {
                return this._prcsTo;
            }
            set
            {
                this._prcsTo = value;
            }
        }

        public int SetLeft
        {
            get
            {
                return this._setLeft;
            }
            set
            {
                this._setLeft = value;
            }
        }

        public int SetTop
        {
            get
            {
                return this._setTop;
            }
            set
            {
                this._setTop = value;
            }
        }
    }
}

